package com.tomek.ktoryoperator.utils;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.tomek.ktoryoperator.R;
import com.tomek.ktoryoperator.activities.MainActivity;
import com.tomek.ktoryoperator.db.NetworkProviderContentProvider;

public class NotificationUtils {
	//private static final int ID_showNotificationOperatorChanged = 1;
	//private static final int ID_showNotificationOperatorAdded = 2;

	public static void showNotificationOperatorChanged(Context context, String who, String number, String oldNetwork,
			String newNetwork) {
		String flowTitle = context.getString(R.string.notification_operator_changed);
		String title = context.getString(R.string.app_name);
		String text = String.format(context.getString(R.string.notification_operator_changed_details), who, number,
				oldNetwork, newNetwork);

		Notification n = new Notification(R.drawable.ic_icon, flowTitle, System.currentTimeMillis());

		Intent i = new Intent(context, MainActivity.class);
		i.setAction(MainActivity.INTENT_ACTION_SCROLL_TO_NUMBER);
		i.putExtra(MainActivity.INTENT_ACTION_SCROLL_TO_NUMBER_PARAM_NUMBER, number);
		i.setData(Uri.withAppendedPath(NetworkProviderContentProvider.CONTENT_ENTRY_BY_NUMBER_URI, number));
		
		PendingIntent pi = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
		n.setLatestEventInfo(context, title, text, pi);
		n.flags = Notification.FLAG_AUTO_CANCEL;

		NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		//nm.notify(ID_showNotificationOperatorChanged, n);
		nm.notify((int) System.currentTimeMillis(), n);
	}

	public static void showNotificationOperatorAdded(Context context, String who, String number, String newNetwork) {
		String flowTitle = context.getString(R.string.notification_operator_new);
		String title = context.getString(R.string.app_name);
		String text = String.format(context.getString(R.string.notification_operator_new_details), who, number,
				Utils.getNetworkTypeString(context, newNetwork));

		Notification n = new Notification(R.drawable.ic_icon, flowTitle, System.currentTimeMillis());

		Intent i = new Intent(context, MainActivity.class);
		i.setAction(MainActivity.INTENT_ACTION_SCROLL_TO_NUMBER);
		i.putExtra(MainActivity.INTENT_ACTION_SCROLL_TO_NUMBER_PARAM_NUMBER, number);
		i.setData(Uri.withAppendedPath(NetworkProviderContentProvider.CONTENT_ENTRY_BY_NUMBER_URI, number));

		PendingIntent pi = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
		n.setLatestEventInfo(context, title, text, pi);
		n.flags = Notification.FLAG_AUTO_CANCEL;

		NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		// nm.notify(ID_showNotificationOperatorAdded, n);
		nm.notify((int) System.currentTimeMillis(), n);
	}
}